<template>
	<view class="delay-list pb-60">
		<view>
			<view class="search">
				<view style="background-color: #fff;margin: 0 10px;border-radius: 5px;height: 42px;" class="uni-flex">
					<view style="-webkit-flex: 1;flex: 1;" class="uni-flex">
						<view class="date-p"><uni-datetime-picker type="date" v-model="createdAt" :border="false"
								placeholder="登记日期" @change="changeDate" />
						</view>
					</view>
				</view>
			</view>
			<view class="uni-mlr-10">
				<z-paging ref="paging" v-model="dataList" @query="queryList">
					<!-- 设置自己的emptyView组件，非必须。空数据时会自动展示空数据组件，不需要自己处理 -->
					<view slot="empty" class="empty text-center text-sm"><text>暂时没有数据~</text>
					</view>
					<view>
						<view class=" uni-mb-5 delay-item uni-mlr-10"
							:style="{paddingBottom:item.planeNo||item.originC||item.destC?'6px':'12px'}"
							v-for="(item,index) in dataList" :key="index" @tap="goUploadCredial(item)">
							<view class="uni-flex uni-row">
								<view class="text uni-flex left-logo">
									<image :src="item.airImageUrl" mode="widthFix"></image>
								</view>
								<view class="uni-flex uni-column right-info">
									<view class="uni-flex" style="justify-content: space-between;">
										<view class="text title" style="flex: 1;">
											{{item.flightNo}}
										</view>
										<view class="text uni-flex right-status">
											<text class="status" style="background-color:#0c6eb7"
												v-if="item.aOrD=='A'">进港</text>
											<text class="status" style="background-color:#FF9800"
												v-if="item.aOrD=='D'">出港</text>
										</view>
									</view>
									<view class="uni-flex uni-row">
										<view class="date">航班日期：{{item.flightDate}}</view>
									</view>

								</view>
							</view>
							<view class="uni-flex uni-row dest" style="margin-top: 5px;"
								v-if="item.planeNo||item.originC||item.destC">
								<view class="item" v-if="item.planeNo">
									<view class="val">{{item.planeNo}}</view>
									<text class="label">机号</text>
								</view>
								<view class="item" v-if="item.originC">
									<view class="val">{{item.originC}}</view>
									<text class="label">出发地</text>
								</view>
								<view class="item" v-if="item.destC">
									<view class="val">{{item.destC}}</view>
									<text class="label">目的地</text>
								</view>
							</view>
							<!-- <view class="uni-flex uni-row dest" v-if="item.dpTime&&item.deTime">
								<view class="item" v-if="item.dpTime">
									<view class="val">{{item.dpTime}}</view>
									<text class="label">计划起飞时间</text>
								</view>
								<view class="item" v-if="item.deTime">
									<view class="val">{{item.deTime}}</view>
									<text class="label">预计起飞时间</text>
								</view>
							</view> -->
						</view>

					</view>
				</z-paging>
			</view>

		</view>
		<!-- 底部按钮 -->
		<view class="bottom-btn-fixed">
			<view>
				<button class="btn-f" type="primary" @click="scanQrCode">扫一扫</button>
				<button class="btn-f btn-f-all" type="primary" @click="viewAll">查看全部</button>
			</view>
		</view>
	</view>
</template>

<script>
	import configService from '@/common/service/config.service.js';

	export default {
		data() {
			return {
				createdAt: '',
				url: {
					list: '/fd/queryv2',
					getFlightImage: '/flightImage/',
				},
				dataList: [],
				flightId: '',
				token: '',
				// airImage:''
			}
		},
		onLoad(e) {
			this.token = uni.getStorageSync('token') || '09a36363e5da4fdfa6d14d1bb7c0078a'
			this.createdAt = this.getDate(Date.now())
			// console.log(this.token);

			console.log(e);
			if (e.flightId) {
				this.flightId = e.flightId;
			}

		},
		onShow() {
			this.queryList(1, 10)
		},
		methods: {
			// ...mapActions(['loginByLoginCode']),
			queryList(pageNo, pageSize) {
				//这里的pageNo和pageSize会自动计算好，直接传给服务器即可
				//这里的请求只是演示，请替换成自己的项目的网络请求，请在网络请求回调中
				//通过this.$refs.paging.complete(请求回来的数组);将请求结果传给z-paging
				let me = this
				var params = {
					page: pageNo,
					limit: pageSize,
					token: this.token,
					flightDate: this.createdAt
				}
				if (this.flightId.length) {
					params.flightId = this.flightId
				}
				this.$http.post(this.url.list, params).then(res => {
					// console.log(res)
					if (res.data.errcode == 0) {
						// me.newsList=res.data.result.records
						let tempData = res.data.data;
						for (let i = 0; i < tempData.length; i++) {
							if (tempData[i].airImage && tempData[i].airImage.length) {
								let split = tempData[i].airImage.split('/')
								let flatN = split[split.length - 1]
								uni.request({
									url: configService.apiUrl.replace(/\/m\//, '') + this.url
										.getFlightImage + flatN,
									responseType: "arraybuffer",
									success: async function(res) {
										// console.log(res);
										let datas = res.data
										//将arrayBuffer数据转换成base64格式即可显示 
										// me.airImage = `data:image/jpeg;base64,${uni.arrayBufferToBase64(datas)}` 
										tempData[i].airImageUrl =
											`data:image/jpeg;base64,${uni.arrayBufferToBase64(datas)}`
									},
								})
							}
						}
						// this.$refs.paging.complete(res.data.data);
						setTimeout(() => {
							this.$refs.paging.complete(res.data.data);
						}, 300)

					} else {
						uni.showToast({
							title: res.data.errmsg,
							icon: "none"
						})
					}
				})
			},
			// 获取航司Logo图片
			getAirImage(url) {
				if (url && url.length) {
					let split = url.split('/')
					let flatN = split[split.length - 1]
					let me = this
					uni.request({
						url: configService.apiUrl.replace(/\/m\//, '') + this.url.getFlightImage + flatN,
						responseType: "arraybuffer",
						success: async function(res) {
							let datas = res.data
							//将arrayBuffer数据转换成base64格式即可显示 
							me.airImage = `data:image/jpeg;base64,${uni.arrayBufferToBase64(datas)}`
							// console.log(me.airImage);
							return me.airImage
						},
					})
				}


			},
			changeDate(e) {
				console.log('----change事件:', e);
				this.flightId = ''
				this.createdAt = e
				this.queryList(1, 10)
			},
			goUploadCredial(item) {
				let copyData = JSON.parse(JSON.stringify(item))
				if (copyData.airImageUrl) delete copyData.airImageUrl
				// copyData.airImageUrl字符串太长了，传递不过去
				uni.navigateTo({
					url: '/pages/index/uploadCredentialAndSign?data=' + JSON.stringify(copyData)
				})
			},
			scanQrCode() {
				let me = this;
				uni.scanCode({
					onlyFromCamera: true,
					scanType: 'qrCode',
					hideAlbum: true,
					success: async function(res) {
						// console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						// loginCode:7yu328ewru238hedj238u2390rudi19u32rf923j|flightId:6tgh9876tghj976tghji976tghjio
						if (res.result.includes('|')) {
							// let loginCode = res.result.split('|')[0].split(':')[1]
							let flightId = res.result.split('|')[1].split(':')[1]
							// console.log(loginCode, flightId);
							// let obj = Object.assign({},  {loginCode: loginCode})
							me.flightId = flightId
							uni.reLaunch({
								url: '/pages/index/delayList?flightId=' + flightId
							})

						} else {
							// res.result.split('|')
							let loginCode = res.result.split(':')[1]
							// let obj = Object.assign({},  {loginCode: loginCode})
							uni.reLaunch({
								url: './delayList'
							})
						}

					}
				})
			},
			viewAll() {
				this.flightId = ''
				uni.reLaunch({
					url: './delayList'
				})
			},
			getDate(date) {
				date = new Date(date)
				const year = date.getFullYear()
				const month = date.getMonth() + 1
				const day = date.getDate()
				return `${year}-${this.addZero(month)}-${this.addZero(day)}`
			},
			addZero(num) {
				if (num < 10) {
					num = `0${num}`
				}
				return num
			},
		}
	}
</script>

<style>
	.delay-list .z-paging-content {
		top: 60px !important;
	}

	/* #ifdef H5 */
	.delay-list .z-paging-content {
		top: 90px !important;
	}

	/* #endif */
</style>